﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Presentation.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;

    public static class ConsignViewModelConverter
    {
        public static void Convert(Consign consign, IConsignViewModel consignViewModel)
        {
            consignViewModel.Type = consign.Type.GetHashCode().ToString().ConvertToTagText(DictionaryIdentifier.GroupType.ConsignType);
            consignViewModel.Experience = consign.Experience.ConvertToTagText();
            consignViewModel.Age = consign.Age.ConvertToTagText();
            consignViewModel.Degree = consign.Degree.ConvertToTagText();
            consignViewModel.Salary = consign.Salary.ConvertToTagText();
            consignViewModel.NeccessarySkill = consign.NecessarySkill.ToPositionArray().ConvertToTagText(DictionaryIdentifier.GroupType.NannySkill);
        }

        public static void ConvertForEdit(IConsignViewModel consignViewModel, Consign consign)
        {
            consignViewModel.IsMarriedForEdit = consign.IsMarried.GetHashCode();
            consignViewModel.HasChildrenForEdit = consign.HasChildren.GetHashCode();
            consignViewModel.TypeForEdit = consign.Type.GetHashCode();
            consignViewModel.RangForEdit = consign.Range.GetHashCode();
            consignViewModel.NeccessarySkills = consign.NecessarySkill.ToStringArray();
        }

        public static void ConvertForEdit(Consign consign, IConsignViewModel consignViewModel)
        {
            consign.NecessarySkill = consignViewModel.NeccessarySkill.ToBinaryValue();
            consign.IsMarried = (ConsignIdentifier.MarriedNeed)consignViewModel.IsMarriedForEdit;
            consign.HasChildren = (ConsignIdentifier.HasChildrenNeed)consignViewModel.HasChildrenForEdit;
        }

        public static IList<IConsignViewModel> Convert(IList<Consign> consigns)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;

            IList<IConsignViewModel> consignViewModels = new List<IConsignViewModel>();
            if (null != consigns)
            {
                foreach (Consign consign in consigns)
                {
                    IConsignViewModel consignViewModel = ModelFactory.CreateModel<IConsignViewModel>();
                    ConsignViewModelConverter.Convert(consign, consignViewModel);
                    consignViewModel.ProprietorID = consign.Proprietor.ID;
                    consignViewModel.ProprietorName = consign.Proprietor.Name;
                    consignViewModel.Avatar = attachmentAppService.GetProprietorAvatar(consign.Proprietor);
                }
            }

            return consignViewModels;
        }
    }
}
